package com.zit.java.controller;

import com.zit.java.pojo.Account;
import com.zit.java.pojo.SysRoles;
import com.zit.java.pojo.SysUser;
import com.zit.java.pojo.ZTreeNode;
import com.zit.java.service.login.LoginService;
import com.zit.java.service.role.RoleService;
import com.zit.java.util.DataUtils;
import com.zit.java.util.object.AjaxDone;
import com.zit.java.util.object.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("role")
public class RoleController  extends  BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private LoginService service;

    /**
     * 到角色列表
     *
     * @return
     */
    @RequestMapping("tolist")
    public String toList() {
        return "role/index";
    }

    /**
     * 返回角色数据
     *
     * @param
     * @return
     */
    @RequestMapping("queryRole")
    @ResponseBody
        public TableData<SysRoles> queryRoles() {
        List<SysRoles> rst = roleService.queryAllRole();
        TableData<SysRoles> td = new TableData<>();

        //使用插件进行分页
//        Page p = PageHelper.startPage(page,limit);
//        PageInfo page = new PageInfo(rst);

        td.setCode(0);
        td.setCount(rst.size());
        td.setData(rst);
        td.setMsg("ok");
        return td;
    }

    /**
     * 到添加角色页面
     *
     * @return
     */
    @RequestMapping("toAddRole")
    public String addRole() {
        return "role/addRole";
    }

    /**
     * 添加角色
     */
                //role/addRole
                //region    添加角色
    @RequestMapping("addRole")
    @ResponseBody
    public AjaxDone saveRole(SysRoles sysRoles, HttpServletRequest request) {
        //获取session中的对象
        Account acc = getLoginAcc(request);
        sysRoles.setCreateUser(acc.getName());
        sysRoles.setCreateDate(DataUtils.getCurrentTime());

        int rst = roleService.saveRole(sysRoles);
        if (rst > 0) {
            return new AjaxDone(200, "添加成功");
        }
            return new AjaxDone(300, "添加失败");
    }
    //endregion
                //region 删除角色
    /**
     * 删除角色
     */
    @DeleteMapping("deleteRole")
    @ResponseBody
    public AjaxDone delRole(String roleId) {
        int rst = roleService.delRole(roleId);

        if (rst > 0) {
            return new AjaxDone(200, "删除成功");
        }
        return new AjaxDone(300, "删除失败");
    }
    //endregion


    /**
     * 到用户修改页面
     */

    @RequestMapping("toUpdateRole")
    public String toUpdateRole(@RequestParam("id") String id, Model model) {
        SysUser sysUser = roleService.selectOneUser(id);
        model.addAttribute("user",sysUser);
        return "role/updateRole";
    }


    /**
     * 修改角色信息
     */
    @RequestMapping("updateRole")
    @ResponseBody
    public AjaxDone updateRole(SysRoles sysRoles, HttpServletRequest request) {
        //讲session的数据绑定到里面
        Account account = getLoginAcc(request);

        sysRoles.setUpdateUser(account.getName());
        sysRoles.setUpdateDate(DataUtils.getCurrentTime());
        int rest = roleService.updateRole(sysRoles);
        if (rest > 0) {
            return new AjaxDone(200, "修改成功");
        }
        return new AjaxDone(300, "修改失败");
    }

    @RequestMapping("toRole")
    public String toUpdatePag(@RequestParam("roleId") String roleId,Model model){
       SysRoles sysRoles =  roleService.queryRoleByRoleId(roleId);
       model.addAttribute("role",sysRoles);
        return "role/edtiRole";
    }


    @RequestMapping("updRole")
    @ResponseBody
    public AjaxDone updRole(SysRoles sysRoles){
      int rest =   roleService.updateRoleByRoleId(sysRoles);
    if (rest>0){
        return new AjaxDone(200,"更新成功");
    }
        return new AjaxDone(300,"更新失败");

    }
    /***
     * 更新角色状态
     * @param roleId
     * @param roleStatus
     * @return
     */
    @RequestMapping("updateStatus")
    @ResponseBody
    public AjaxDone updateStatus(Integer roleId, Integer roleStatus) {
        int rst = roleService.updateStatus(roleId, roleStatus);
        if (rst > 0) {
            return new AjaxDone(200, "更新状态成功");
        }
            return new AjaxDone(300, "更新状态失败");
    }

    /**
     * 获取后台id展示到前端页面
     */
    @RequestMapping("showRoleId")
    @ResponseBody
    public AjaxDone showRoleId() {
        List<SysRoles> rst = roleService.showRoleId();
        return new AjaxDone(200, "成功", rst);
    }
    /**
     * 到设置角色权限页面
     */

    @RequestMapping("toSetRights")
    public String toSetRights(@ModelAttribute("roleId") String roleId) {
        return "role/setRights";
    }

    /**
     * 查询zTree的数据
     */

    @RequestMapping("queryRights")
    @ResponseBody
    public List<ZTreeNode> createTree(@RequestParam String roleId){
      List<ZTreeNode> zTreeNodes =  roleService.createTree("0",roleId);
        return  zTreeNodes;
    }

    /**
     * 报存角色授权信息
     * @param roleId
     * @param rightsIds
     * @return
     */
    @RequestMapping("saveRolesRights")
    @ResponseBody
    public AjaxDone saveRolesRights(String roleId,@RequestParam("rightsIds[]") String [] rightsIds,HttpServletRequest request){
            int  rst =     roleService.saveRoleRights(roleId,rightsIds);

            //刷新局部页面,原理就是重新绑定session对象
             Account account =           getLoginAcc(request);
            //获取application
             ServletContext servletContext  = request.getServletContext();
             //获取session
        HttpSession session = (HttpSession) servletContext.getAttribute(String.valueOf(account.getId()));
        //保存角色信息 重新绑定session
        Map<String,Integer> allRightsBtn =  service.queryAllRightsBtn((int)account.getId());
        //获取session对象,把session对象放到jsp页面中
        session.setAttribute("btnRights",allRightsBtn);

        if (rst>0){
                return new AjaxDone(200,"授权成功!");
            }
                return new AjaxDone(300,"授权失败!");
    }

}
